#!/usr/bin/env node
const vm = require('vm');
const moment = require('moment');
const _ = require('lodash');

const args = process.argv.slice(2);
if (!args[0] || args[0] === '-h' || args[0] === '--help') {
    console.log("Usage:");
    console.log("      xc [1+2]/3");
    console.log("      xc abs(-1)+pow(2,3)");
    console.log("      xc _.random(1,5)+moment()+date()+time()");
    console.log("      Math: abs,ceil,floor,round,min,max,sqrt,pow,exp,sin,cos,tan");
}
try {
    var params = args.join('');
    params = params.replace(/\[/g, '(').replace(/]/g, ')');
    params = params.replace(/（/g, '(').replace(/）/g, ')');
    params = params.replace(/”|“/g, '"').replace(/’|‘/g, "'");
    params = params.replace(/\【/g, '(').replace(/】/g, ')');
    params = params.replace(/{/g, '(').replace(/}/g, ')');
    params = params.replace(/x/g, '*');
    params = params.replace(/abs/g, 'Math.abs').replace(/ceil/g, 'Math.ceil').replace(/floor/g, 'Math.floor').replace(/round/, 'Math.round').replace(/min/, 'Math.min').replace(/max/, 'Math.max').replace(/sqrt/, 'Math.sqrt').replace(/pow/, 'Math.pow').replace(/sin/, 'Math.sin').replace(/cos/, 'Math.cos').replace(/tan/, 'Math.tan').replace(/exp/, 'Math.exp').replace(/Math\.Math/g, 'Math')
    vm.runInNewContext(`console.log(${params})`, {
        _,
        moment,
        console,
        date: o => moment(o).format('YYYY-MM-DD'),
        time: o => moment(o).format('YYYY-MM-DD HH:mm:ss'),
    });
} catch (e) {
    console.log('长度:' + params.length);
}
